﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.BussinessLogic;
namespace QUANLIHOCSINH
{
    enum Status_QuanLiNguoiDung { View, Insert, Update, Delete };
    public partial class FrmQuanLiNguoiDung : Form
    {
        NGUOIDUNGBUS nguoidung = new NGUOIDUNGBUS();
        GIOITINHBUS gioitinh = new GIOITINHBUS();
        NHOMNGUOIDUNGBUS nhom = new NHOMNGUOIDUNGBUS();
        Status_QuanLiNguoiDung TrangThai = Status_QuanLiNguoiDung.View;
        public FrmQuanLiNguoiDung()
        {
            InitializeComponent();
        }
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
            btnXoa.Visible = !Enable;
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;

            gbmonhoc.Enabled = Enable;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            ResetText();
            txtMaNguoiDung.Text = nguoidung.NextID();
            EnableButton(true);
            btnThem.Visible = false;
          TrangThai = Status_QuanLiNguoiDung.Insert;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvNguoiDung.Rows.Count > 1)
            {
                int r = dgvNguoiDung.CurrentRow.Index;
                txtMaNguoiDung.Text = dgvNguoiDung.Rows[r].Cells["MaNguoiDung"].Value.ToString();
                txtMatKhau.Text = dgvNguoiDung.Rows[r].Cells["MatKhau"].Value.ToString();
                txtTenNguoiDung.Text = dgvNguoiDung.Rows[r].Cells["TenNguoiDung"].Value.ToString();
                txtDiaChi.Text = dgvNguoiDung.Rows[r].Cells["DiaChi"].Value.ToString();
                cbGioiTinh.SelectedValue = dgvNguoiDung.Rows[r].Cells["ColGioiTinh"].Value.ToString();
                cbNhomNguoiDung.SelectedValue = dgvNguoiDung.Rows[r].Cells["Nhom"].Value.ToString();
            }
            EnableButton(true);
            btnSua.Visible = false;
            TrangThai = Status_QuanLiNguoiDung.Update;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("Bạn Có Muốn Xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                NGUOIDUNG nd = new NGUOIDUNG();
                int r = dgvNguoiDung.CurrentCell.RowIndex;
                nd.MaNguoiDung= dgvNguoiDung.Rows[r].Cells[0].Value.ToString();
                nguoidung.XoaNguoiDung(nd);
                FrmQuanLiNguoiDung_Load(sender, e);
            }
        }
        private NGUOIDUNG LayThongTinNguoiDung()
        {
            NGUOIDUNG nd = new NGUOIDUNG();
            nd.MaNguoiDung = txtMaNguoiDung.Text;
            nd.MatKhau = txtMatKhau.Text;
            nd.TenNguoiDung = txtTenNguoiDung.Text;
            nd.DiaChi = txtDiaChi.Text;
            nd.GioiTinh = cbGioiTinh.SelectedValue.ToString();
            nd.MaNhom = cbNhomNguoiDung.SelectedValue.ToString();
            return nd;
        }
        public void ResetText()
        {
            txtDiaChi.Text = "";
            txtMatKhau.Text = "";
            txtTenNguoiDung.Text = "";
         
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (TrangThai == Status_QuanLiNguoiDung.Insert)
            {
                if (nguoidung.ThemNguoiDung(LayThongTinNguoiDung()))
                {
                    FrmQuanLiNguoiDung_Load(sender, e);
                }
            }
            if (TrangThai == Status_QuanLiNguoiDung.Update)
            {
                if (nguoidung.SuaNguoiDung(LayThongTinNguoiDung()))
                {
                    FrmQuanLiNguoiDung_Load(sender, e);
                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            EnableButton(false);
        }

        private void FrmQuanLiNguoiDung_Load(object sender, EventArgs e)
        {
            (dgvNguoiDung.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).DataSource = gioitinh.LayDanhSachGioiTinh();
            (dgvNguoiDung.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).DisplayMember = "TenGioiTinh";
            (dgvNguoiDung.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).ValueMember = "GioiTinh";

            (dgvNguoiDung.Columns["Nhom"] as DataGridViewComboBoxColumn).DataSource = nhom.LayDanhNhomNguoiDungThuong(); ;
            (dgvNguoiDung.Columns["Nhom"] as DataGridViewComboBoxColumn).DisplayMember = "TenNhom";
            (dgvNguoiDung.Columns["Nhom"] as DataGridViewComboBoxColumn).ValueMember = "MaNhom";

            cbGioiTinh.DataSource = gioitinh.LayDanhSachGioiTinh();
            cbGioiTinh.DisplayMember = "TenGioiTinh";
            cbGioiTinh.ValueMember = "GioiTinh";

            cbNhomNguoiDung.DataSource = nhom.LayDanhNhomNguoiDungThuong(); ;
            cbNhomNguoiDung.DisplayMember = "TenNhom";
            cbNhomNguoiDung.ValueMember = "MaNhom";


            dgvNguoiDung.DataSource = nguoidung.LayDanhSachNguoiDungThuong();
            EnableButton(false);
        }

        private void lbTieuDeMonHoc_Click(object sender, EventArgs e)
        {

        }
    }
}
